var pageii = null;
var grid = null;
$(function() {
	grid = lyGrid({
		id : 'paging',
		l_column : [ {
			colkey : "id",
			name : "id",
			width : "50px",
			hide : true
		}, {
			colkey : "imgpath",
			width : "200px",
			name : "活动图片",
			renderData : function(rowindex ,data, rowdata, colkey){
				if(data) {
					return '<img src="' + rootPath + data + '" width="150px"/>';
				}else {
					return data;
				}
			}
		},  {
			colkey : "title",
			name : "标题"
		},{
			colkey : "content",
			name : "内容"
		},{
			colkey : "startTime",
			name : "开始时间",
			renderData : function(rowindex,data, rowdata, column) {
				return new Date(data).format("yyyy-MM-dd hh:mm");
			}
		},{
			colkey : "endTime",
			name : "结束时间",
			renderData : function(rowindex,data, rowdata, column) {
				return new Date(data).format("yyyy-MM-dd hh:mm");
			}
		},{
			colkey : "type",
			name : "状态",
			renderData : function(rowindex, data, rowdata, column) {
				if ("1" == data) {
					return "微信开启"
				} else {
					return "微信关闭";
				}}
		}],
		jsonUrl : rootPath + '/luckydraw/findByPage.shtml',
		checkbox : true
	});
	$("#searchForm").click("click", function() {// 绑定查询按扭
		var searchParams = $("#fenye").serializeJson();
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addFun").click("click", function() {
		addFun();
	});
	$("#editFun").click("click", function() {
		editFun();
	});
	$("#setingFun").click("click", function() {
		setingFun();
	});
	$("#delFun").click("click", function() {
		delFun();
	});
	$("#typeFun").click("click", function() {
		typeFun();
	});
	
});

function permissions() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("请选择一个对象！");
		return;
	}
}

function addFun() {
	pageii = layer.open({
		title : "新增",
		type : 2,
		area : [ "600px", "60%" ],
		content : rootPath + '/luckydraw/addUI.shtml'
	});
}

function editFun() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	pageii = layer.open({
		title : "编辑",
		type : 2,
		area : [ "600px", "60%" ],
		content : rootPath + '/luckydraw/editUI.shtml?id=' + cbox
	});
}

function typeFun() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	
	layer.confirm('是否开启活动？', function(index) {
		var url = rootPath + '/luckydraw/openEntity.shtml?id=' + cbox;
		var s = CommnUtil.ajax(url, "", "json");
		if (s == "success") {
			layer.msg('开启成功');
			grid.loadData();
		} else {
			layer.msg('开启失败');
		}
	});
}


function delFun() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/luckydraw/deleteEntity.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}


function setingFun() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	pageii = layer.open({
		title : "设置活动",
		type : 2,
		area : [ "600px", "60%" ],
		content : rootPath + '/luckydraw/setingUI.shtml?pid=' + cbox
	});
}
